package com.crl.animation.resource
{
	import com.crl.events.RemoteEvent;
	import com.crl.net.NetStatus;
	import com.crl.net.ResourceToken;
	
	public class AbstractResource
	{
		/**
		 * 文件路径; 
		 */		
		public var path:String;
		/**
		 * 状态; 
		 */		
		protected var _status:int=0;
		
		
		public function AbstractResource()
		{
		}
		
		public function load(path:String):ResourceToken{
			_status=NetStatus.LOADING;
			var token:ResourceToken=getToken(path);
			if(token)bindTokenEvent(token,true);
			
			return token;
		}
		
		public function getToken(path:String):ResourceToken{
			return null;
		}
		
		
		private function bindTokenEvent(token:ResourceToken,bool:Boolean):void{
			if(bool){
				token.addEventListener(RemoteEvent.SUCCESED,successHandler);
				token.addEventListener(RemoteEvent.FAILED,failedHandler);
			}else{
				token.removeEventListener(RemoteEvent.SUCCESED,successHandler);
				token.removeEventListener(RemoteEvent.FAILED,failedHandler);
			}
		}
		
		protected function successHandler(event:RemoteEvent):void{
			bindTokenEvent(event.target as ResourceToken,false);
			_status=NetStatus.COMPLETE;
		}
		
		protected function failedHandler(event:RemoteEvent):void{
			_status=NetStatus.FAILED;
			bindTokenEvent(event.target as ResourceToken,false);
		}
	}
}